﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;
namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OJobReport : PageBase
    {

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OJobReport";
            }
        }

        protected int Year
        {
            get
            {
                return GetRequestValue<int>("year", DateTime.Now.Year);
            }
        }
        protected int Month
        {
            get
            {
                return GetRequestValue<int>("Month", DateTime.Now.Month);
            }
        }
        protected string colMonth
        {
            get
            {
                return Year.ToString()+"-"+Month.ToString();
            }
        }
        protected DateTime stated
        {
            get
            {
                return Convert.ToDateTime(colMonth);
            }
        }

        protected DateTime endd
        {
            get
            {
                return stated.AddMonths(1).AddDays(-1);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //GV55NSL.DataSource = GetMacReport("55NSL","");
            //GV55NSL.DataBind();

            //GV65NS.DataSource = GetMacReport("65NS","");
            //GV65NS.DataBind();

            //GV80NS.DataSource = GetMacReport("80NS","");
            //GV80NS.DataBind();

            //GV65NSLGreen.DataSource = GetMacReport("65NSL", "绿");
            //GV65NSLGreen.DataBind();

            //GV65NSLYellow.DataSource = GetMacReport("65NSL", "黄");
            //GV65NSLYellow.DataBind();
            this.AddLog(Permission.查看);
        }

        protected string GetHtmlMacReport(string mactype, string color)
        {
            DataTable dt = GetMacReport(mactype, color);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<table class='list' width='90%'>");

            //1.表头
            sb.AppendFormat("<thead><tr>");
            for(int i =0; i < dt.Columns.Count; i++)
            {                
                sb.AppendFormat("<th {1}>{0}</th>",dt.Columns[i].ColumnName, i==0?"colspan='2'":"");
            }
            sb.AppendFormat("</tr></thead>");
            //2.各行
            sb.AppendFormat("<tbody>");
            for (int r =0;r<dt.Rows.Count;r++ )
            {

                DataRow dr = dt.Rows[r];

                sb.AppendFormat("<tr class='{0}'>", r % 2 == 1 ? "hot" : "");
                string drfirstvalue = dr[0].ToString();
                switch (drfirstvalue)
                { 
                    case "下线目标":
                        sb.AppendFormat("<td {1} ><b>{0}</b></td><td><b>{2}</b></td>", "下线", "rowspan='2'", "目标");
                        break;
                    case "入库目标":
                        sb.AppendFormat("<td {1}><b>{0}</b></td><td><b>{2}</b></td>", "入库", "rowspan='2'", "目标");
                        break;
                    case "下线实项":                       
                    case "入库实项":
                        sb.AppendFormat("<td {1}><b>{0}</b></td>", "实项", "");
                        break;
                    default:
                        sb.AppendFormat("<td {1}><b>{0}</b></td>", drfirstvalue, "colspan='2'");
                        break;
                }

                for (int i =1; i < dt.Columns.Count; i++)
                {
                    sb.AppendFormat("<td >{0}</td>", dr[i]);
                }
                sb.AppendFormat("</tr>");
            }
            sb.AppendFormat("</tbody>");
            sb.AppendLine("</table>");

            return sb.ToString();
        }

        protected DataTable GetMacReport(string mactype,string color)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(string.Format("{0}年{1}月",Year,Month), typeof(string));

            // DateTime stated = new DateTime(2011, 1, 1);
            // DateTime endd = stated.AddMonths(1).AddDays(-1);
            DateTime everyday = stated;

            // Response.Write(endd);
            string colstatelable = "01";
            while (everyday <= endd)
            {
                if (everyday.DayOfWeek == DayOfWeek.Sunday)
                {
                    colstatelable = colstatelable + "-" + everyday.ToString("dd");
                    dt.Columns.Add(colstatelable, typeof(string));
                    colstatelable = everyday.AddDays(1).ToString("dd");
                }
                everyday = everyday.AddDays(1);
            }
            if (endd.DayOfWeek != DayOfWeek.Sunday)
            {
                colstatelable = colstatelable + "-" + endd.ToString("dd");
                dt.Columns.Add(colstatelable, typeof(string));
            }
            dt.Columns.Add("合计", typeof(string));
            ////
            //foreach (DataColumn dc in dt.Columns)
            //{
            //    Response.Write(dc.ColumnName + "<br/>");
            //}
            /////
            string[] items = new string[] { "下线目标", "下线实项", "入库目标", "入库实项", "出库", "库存", "下线未入库" };
            foreach (string item in items)
            {
                DataRow dr = dt.NewRow();
                dr[0] = item;
                for (int i = 1; i < dt.Columns.Count-1; i++)
                {
                    object dcvalue = 0;
                    if (item == "下线目标" || item == "入库目标")
                    {
                        dcvalue = OJobController.OJobGoal_Get(GetInputName(mactype + color, item, dt.Columns[i].ColumnName));
                        dcvalue = dcvalue is DBNull || dcvalue == null ? "0" : dcvalue;
                    }
                    else
                    {
                         dcvalue = getdate(mactype, dr[0].ToString(), dt.Columns[i].ColumnName, color);
                    }
                    dr[i] = dcvalue;
                    
                }
                dt.Rows.Add(dr);
            }
            //计算合计列
            
            foreach (DataRow dr in dt.Rows)
            {
                int hjcount = 0;

                if (dr[0].ToString() == "下线未入库")
                {
                    dr[dt.Columns.Count - 1] = getdate2(mactype, "下线未入库合计", color);
                    continue;
                }
                if (dr[0].ToString() == "库存")
                {
                    
                    continue;
                }
                for (int i = 1; i < dt.Columns.Count - 1; i++)
                {
                    hjcount = hjcount + Convert.ToInt32( dr[i] is DBNull?"0": dr[i].ToString());
                }
                dr[dt.Columns.Count - 1] = hjcount;
            }
            //
            return dt;            
        }
        /// <summary>
        /// 获取输入框值
        /// </summary>
        /// <param name="mactypecolor"></param>
        /// <param name="item"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        private string GetInputName(string mactypecolor, string item, string column)
        {
            return string.Format("{0}{1}{2}{3}{4}", mactypecolor, item, column, Year, Month);
        }

        protected object getdate(string mactype, string itemtype, string colname, string color)
        {           
            string statedate =colMonth+"-"+colname.Split(new char[]{'-'})[0];
            string enddate = colMonth + "-" + colname.Split(new char[] { '-' })[1]+" 23:59:59";

            if (Convert.ToDateTime(statedate) > DateTime.Now)
                return DBNull.Value;
            return OJobController.GetReport(mactype, itemtype, statedate, enddate, color);
        }

        protected object getdate2(string mactype, string itemtype,string color)
        {
            string statedate = colMonth + "-01 00:00:00";
            string enddate = Convert.ToDateTime(statedate).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59");
            return OJobController.GetReport(mactype, itemtype, statedate, enddate, color);
        }

        protected string GetYearList()
        {
            string liTemplete = "<option value='{0}' {1}>{0}年</option>";
            string rHtml = "";
            int nowYear = DateTime.Now.Year;
            for (int i = (nowYear - 3); i <= nowYear; i++)
            {
                rHtml = rHtml + string.Format(liTemplete, i, i == Year ? "selected" : "");
            }
            return rHtml;
        }

        protected object GetTotalMacStock()
        {
            return Convert.ToInt32(GetTotalMacStock("")) - Convert.ToInt32(getdate("18VX", "整机库存", "1-1", ""));
        }
        protected object GetTotalMacStock(string color)
        {        

            int mtotal = 0;
            foreach (string tempmac in macTypeList)
            {
                mtotal += Convert.ToInt32(getdate(tempmac, "整机库存", "1-1", color));
            }
            

            return mtotal ;
        }

        protected object GetTotal(string ItemType)
        {
           // string statedate = colMonth + "-" + colname.Split(new char[] { '-' })[0];
         //   string enddate = colMonth + "-" + colname.Split(new char[] { '-' })[1] + " 23:59:59";


            int mtotal = 0;
            foreach (string tempmac in macTypeList)
            {
                mtotal += Convert.ToInt32(OJobController.GetReport(tempmac, ItemType, stated.ToString("yyyy-MM-dd"), endd.ToString("yyyy-MM-dd 23:59:59"), ""));
            }


            return mtotal;
        }
        /// <summary>
        /// 进口机库存机
        /// </summary>
        /// <returns></returns>
        protected object GetJKTotalMacStock()
        {
            //进口机库存机 17vx3,17vx,19vxt
            //string[] jkMac = new string[] { "17VX301", "18VX", "20VXT","18VX001C" };

            int jktotal = OJobJinKouController.GetSum("",2);
            //foreach (string tempmac in jkMac)
            //{
            //    jktotal += Convert.ToInt32(SBOController.Get_W01001_OnHand(tempmac));
            //}
            return jktotal;
        }
    }
}